package com.jyh.blog.controller;

import com.jyh.blog.annotation.AccessLimit;
import com.jyh.blog.dto.UserAreaDTO;
import com.jyh.blog.dto.UserBackDTO;
import com.jyh.blog.service.UserService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户账号管理")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param userMail
     * @return
     */
    @AccessLimit(seconds = 60,maxCount = 1)
    @ApiOperation(value = "发送验证码")
    @GetMapping("user/code")
    public Result sendCode(String userMail){
        userService.sendCode(userMail);
        return Result.ok();
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public Result register(@Valid @RequestBody UserVO user){
        userService.register(user);
        return Result.ok();
    }

    /**
     * 后台用户列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "后台用户列表")
    @GetMapping("admin/users")
    public Result<PageResult<UserBackDTO>> userBackList(ConditionVO conditionVO){
        return Result.ok(userService.userBackList(conditionVO));
    }

    /**
     * 统计区域用户量
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "统计区域用户量")
    @GetMapping("admin/user/area")
    public Result<List<UserAreaDTO>> userArea(ConditionVO conditionVO){
        return Result.ok(userService.userAreaList(conditionVO));
    }

    /**
     * 修改用户密码
     * @param userVO
     * @return
     */
    @ApiOperation("修改用户密码")
    @PostMapping("user/password")
    public Result updateUserPassword(@Valid @RequestBody UserVO userVO){
        userService.updateUserPassword(userVO);
        return Result.ok();
    }

    /**
     * 修改管理员密码
     * @param passwordVO
     * @return
     */
    @ApiOperation("修改管理员密码")
    @PostMapping("admin/user/password")
    public Result updateAdminPassword(@Valid @RequestBody PasswordVO passwordVO){
        userService.updateAdminPassword(passwordVO);
        return Result.ok();
    }

    /**
     * QQ登录
     * @param qqLoginVO
     * @return
     */
    @ApiOperation("QQ登录")
    @PostMapping("/user/oauth/qq")
    public Result QQLogin(@RequestBody @Valid QQLoginVO qqLoginVO){
        return Result.ok(userService.QQLogin(qqLoginVO));
    }
}
